package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public SysResult fileUpload(MultipartFile file){
        ImageVO imageVO=fileService.upload(file);
        if (imageVO==null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }
    /**
     * 文件删除
     */
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }






    /*//入门教程  无用可删除
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        //1.文件名称
        String filename = file.getOriginalFilename();
        //2.准备文件目录
        String dir="D:/images";
        File dirFile=new File(dir);
        if (!dirFile.exists()){//判断目录是否存在
            dirFile.mkdirs();//创建多级目录
        }
        //3.全路径
        String path=dir+"/"+filename;
        //4.上传
        file.transferTo(new File(path));
        return SysResult.success();
    }*/
}
